package acheng1314.cn.a3dbuild2.view.activity;


import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.acheng.achengutils.mvp.view.BaseActivity;
import com.acheng.achengutils.utils.SPHelper;
import com.acheng.achengutils.widgets.MustDoThingDailog;
import com.acheng.achengutils.widgets.NoScrollListView;

import java.util.ArrayList;
import java.util.List;

import acheng1314.cn.a3dbuild2.MyApplication;
import acheng1314.cn.a3dbuild2.R;
import acheng1314.cn.a3dbuild2.adapter.BuildListAdapter;
import acheng1314.cn.a3dbuild2.bean.BuildBean;
import acheng1314.cn.a3dbuild2.bean.ModelUrlBean;
import acheng1314.cn.a3dbuild2.bean.ProjectItemBean;
import acheng1314.cn.a3dbuild2.hostApi.MyApi;
import acheng1314.cn.a3dbuild2.view.activity.presenter.DetailActivityPresenter;
import acheng1314.cn.a3dbuild2.view.activity.viewcontroller.DetailActivityController;
import acheng1314.cn.a3dbuild2.widgets.MyProgressDialog;

public class DetailActivity extends BaseActivity<DetailActivityPresenter, DetailActivityController> implements DetailActivityController {

    private ProjectItemBean.ResultBean bean;
    private MyProgressDialog myProgressDialog;
    private NoScrollListView mLv_build_list;
    private ArrayList<BuildBean.ResultBean> mListData = new ArrayList<>();
    private BuildListAdapter adapter;
    private String token;
    private String userId;

    @Override
    protected DetailActivityPresenter initPresenter() {
        return new DetailActivityPresenter(this);
    }

    @Override
    protected void initEvent() {
        mLv_build_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent startActivity = new Intent(getContext(), AppWebActivity.class);
                String urlStr = MyApi.ModulApi + "?uid=" + userId + "&bid=" + mListData.get(position).getBid() + "&token=" + token;
                ModelUrlBean urlBean = new ModelUrlBean();
                urlBean.setUid(userId);
                urlBean.setToken(token);
                urlBean.setLatitu(mListData.get(position).getLatitu());
                urlBean.setLongit(mListData.get(position).getLongit());
                startActivity.putExtra(getString(R.string.url), urlStr);
                startActivity.putExtra("urlBean", urlBean);
                startActivity(startActivity);
            }
        });
    }

    @Override
    protected void initView() {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        bean = (ProjectItemBean.ResultBean) getIntent().getSerializableExtra(getString(R.string.user));

        if (bean == null) {
            toolbar.setTitle("项目详情");
        } else {

            toolbar.setTitle("项目:" + bean.getName());
        }

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        TextView mTv_build_intro = (TextView) findViewById(R.id.mTv_build_intro);
        mTv_build_intro.setText(bean.getIntro());

        mLv_build_list = (NoScrollListView) findViewById(R.id.mLv_build_list);

        adapter = new BuildListAdapter(mListData, getContext());

        mLv_build_list.setAdapter(adapter);

    }

    /**
     * get data 4 net
     */
    private void getData4Net() {
        token = SPHelper.getString(getContext(), getString(R.string.user), getString(R.string.token));
        userId = SPHelper.getString(getContext(), getString(R.string.user), getString(R.string.userId));
        if (null == bean) {
            showDailog("内部数据错误");
            return;
        }
        String pid = bean.getPid();
        mPresenter.loadListData(userId, pid, token);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData4Net();
    }

    @Override
    public void showProgressD() {
        if (null == myProgressDialog)
            myProgressDialog = new MyProgressDialog("加载中", "正在加载数据···", getContext());
        else
            myProgressDialog.show();
    }

    @Override
    public void disProgressD() {
        if (null != myProgressDialog)
            myProgressDialog.dismiss();
    }

    @Override
    public void showDailog(String msg) {
        new MustDoThingDailog("提示", msg, getContext(), new MustDoThingDailog.NeedDoThing() {
            @Override
            public void mustDoThings() {

            }
        });
    }

    @Override
    public void updateList(List<BuildBean.ResultBean> result) {
        if (mListData == null) {
            mListData = new ArrayList<>();
        }

        if (adapter == null) {
            adapter = new BuildListAdapter(mListData, getContext());
        }

        if (mListData.size() > 0) {
            mListData.clear();
            mListData.addAll(result);
        } else {
            mListData.addAll(result);
        }

        adapter.notifyDataSetChanged();

        MyApplication.getInstance().outLog("输出", "size:\t" + adapter.getCount());
    }


    @Override
    protected int setLayoutId() {
        return R.layout.activity_detail;
    }

}
